package dbexp.connection;

import static dbexp.model.db.DbOT.TABLE;
import static dbexp.utils.Error.sql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dbexp.connection.model.ConnectionInfo;
import dbexp.model.DbObject;
import dbexp.model.Table;
import dbexp.model.User;

public class OracleConnection extends DbConnection {
	public OracleConnection(final ConnectionInfo ci, Connection con)
			throws PoolManagerException, DbStateException, DbAccessException,
			SQLException
	{
		super(ci, con);
	}
	
	public List<Table> getTables(final String schema, final String tableName)
			throws PoolManagerException, DbAccessException, DbStateException,
			DbQueryException, SQLException
	{
		final List<Table> list = new ArrayList<Table>();
		ResultSet rs = null;
		String query = "SELECT OWNER,TABLE_NAME,TABLESPACE_NAME,DEGREE,TABLE_LOCK,TEMPORARY FROM ALL_ALL_TABLES WHERE OWNER=UPPER(?)";
		if (tableName != null) {
			query += " AND TABLE_NAME=?";
		}
		PreparedStatement ps = null;
		try {
			ps = prepareStatement(query);
			ps.setString(1, schema);
			if (tableName != null) {
				ps.setString(2, tableName);
			}
			rs = ps.executeQuery();
			while (rs.next()) {
				final String name = rs.getString("TABLE_NAME");
				if (containsExtraName(name)) {
					continue;
				}
				final Table table = new Table(name, rs.getString("OWNER"));
				
				table.setTableSpace(rs.getString("TABLESPACE_NAME"));
				table.setDegree(rs.getString("DEGREE"));
				table.setTableLock(rs.getString("TABLE_LOCK"));
				table.setTemporary(rs.getString("TEMPORARY"));
				table.setTyp(TABLE);
				list.add(table);
			}
			return list;
		}
		catch (final PoolManagerException dbme) {
			System.err.println("ERROR:" + dbme.getMessage());
		}
		catch (final SQLException sqle) {
			sqle.printStackTrace();
			sql.throwDbStateException(sqle);
		}
		finally {
			cC(rs, ps);
		}
		return new ArrayList<Table>();
	}
	
	public User getUser(final String name) throws PoolManagerException,
			DbAccessException, DbStateException, DbQueryException, SQLException
	{
		final User out = new User(name);
		ResultSet rs = null;
		final String query = "SELECT * FROM DBA_USERS WHERE USERNAME=? AND ROWNUM=1";
		PreparedStatement ps = null;
		try {
			ps = prepareStatement(query);
			ps.setString(1, name);
			rs = ps.executeQuery();
			if (rs.next()) {
				out.setStatus(rs.getString("ACCOUNT_STATUS"));
				out.setProfile(rs.getString("PROFILE"));
				out.setCreated(rs.getString("CREATED"));
				out.setDefaultTableSpace(rs.getString("DEFAULT_TABLESPACE"));
				out.setTemporaryTableSpace(rs.getString("TEMPORARY_TABLESPACE"));
				loadPrivs(out);
			}
			return out;
		}
		catch (final PoolManagerException dbme) {
			System.err.println("ERROR:" + dbme.getMessage());
		}
		catch (final SQLException sqle) {
			sqle.printStackTrace();
			sql.throwDbStateException(sqle);
		}
		finally {
			cC(rs, ps);
		}
		return new User("NOTFOUND");
	}
}
